<?php

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$key = 'tmp.lock';

$lock = lock($key);   //获取锁

$data = [
	'get_lock' =>  $lock,
	'ttl' => $redis->ttl($key),
	'data' => $redis->get($key)
];
var_dump($data);
lock($key, 0);       //释放锁


function lock($key, $timeOut = 60) {
    $redis = $GLOBALS['redis'];
	if (0 >= $timeOut) return $redis->del($key) || true;
    //$rt = $redis->setnx($key, 1);
    $rt = $redis->set($key, 1, ['NX', 'EX' => $timeOut]);
    return $rt;
}